package com.monitorRoutine.monitorRoutineController.util;

import lombok.extern.java.Log;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Log
public class YamlReaderUtil {
    public static Map<String, String > readConfig(String yamlFilePath, String[] params) throws Exception {
        Yaml yaml = new Yaml();
        Map<String, Object> config = new HashMap<>();


        try (InputStream inputStream = new FileInputStream(yamlFilePath)) {
            // 解析YAML文件到Map
            config = yaml.load(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
            log.severe("配置参数 " + yamlFilePath + " 解析错误。");
            throw new Exception();
        }

        // 创建一个新的Map来存放所需的配置参数
        Map<String, String> result = new HashMap<>();
        for (String param : params) {
            // 检查配置文件中是否存在指定的参数键
            if (config.containsKey(param)) {
                result.put(param, (String) config.get(param));
            } else {
                log.severe("配置参数 " + param + " 不存在于YAML文件中。");
                throw new Exception();
            }
        }

        return result;
    }

}